package com.amuse.amuse.mapper.campus_permissions;

import com.amuse.amuse.entity.campusCirclePosts;
import com.amuse.amuse.entity.postComments;
import com.amuse.amuse.entity.postLikes;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper

public interface CampusPermissionsDao {
    /*
    * 管理员和用户写帖子
    * */
    @Insert("insert into campuscircleposts(content,postId,studentId,publishDate) values(#{content},#{postId},#{studentId},#{publishDate})")
    int addPosts(@Param("content") String content, @Param("postId") Integer postId, @Param("studentId") Integer studentId, @Param("publishDate") String publishDate);

    /*
    * 管理员和用户删帖子
    * */
    @Insert("delete from campuscircleposts where postId = #{postId}")
    int deletePosts(@Param("postId") Integer postId);

    /*
    * 管理员和用户修改帖子
    * */
    @Insert("update campuscircleposts set content = #{content},publishDate =#{publishDate} where postId = #{postId}")
    int updatePosts(@Param("content") String content, @Param("publishDate") String publishDate, @Param("postId") Integer postId);

    /*
    * 管理员和用户查看自己所发帖子
    * */

    @Select("select * from campuscircleposts where studentId = #{studentId}")
    List<campusCirclePosts> findPostsByStudentId(@Param("studentId") Integer studentId);



    /*
    *查看帖子下面的评论
    *
    * */
    @Select("select * from postcomments where postId = #{postId}")
    List<postComments> findCommentsByPostId(@Param("postId") Integer postId);




    /*
    * 管理员查看所有帖子以及个帖子的评论数和点赞数
    * */
    @Select("SELECT " +
            "    p.postId," +
            "    p.studentId AS post_studentId," +
            "    p.content AS post_content," +
            "    p.publishDate AS post_publishDate," +
            "    COUNT(DISTINCT c.commentId) AS commentCount," +
            "    COUNT(DISTINCT l.likeId) AS likeCount " +
            "FROM " +
            "    campusCirclePosts p " +
            "LEFT JOIN " +
            "    postComments c ON p.postId = c.postId " +
            "LEFT JOIN " +
            "    postLikes l ON p.postId = l.postId " +
            "GROUP BY " +
            "    p.postId, p.studentId, p.content, p.publishDate;")
    List<campusCirclePosts> findAllPosts();



    /*
    * 用户给某条帖子评论
    * */
    @Insert( "insert into postcomments(content,commentId,postId,studentId,publishDate) values(#{content},#{commentId},#{postId},#{studentId},#{publishDate}) ")
    int addComments(@Param("content") String content, @Param("commentId") Integer commentId, @Param("postId") Integer postId, @Param("studentId") Integer studentId, @Param("publishDate") String publishDate);

    /*
    *
    * 用户删除自己的评论
    * */
    @Insert("delete from postcomments where commentId = #{commentId}")
    int deleteComments(@Param("commentId") Integer commentId);

    /*
    * 用户查看自己的评论以及帖子帖子信息
    *
    * */
    @Select("\tSELECT \n" +
            "\t\t\tp.postId,\n" +
            "\t\t\tp.studentId AS post_studentId,\n" +
            "\t\t\tp.content AS post_content,\n" +
            "\t\t\tp.publishDate AS post_publishDate,\n" +
            "\t\t\tc.commentId,\n" +
            "\t\t\tc.studentId AS comment_studentId,\n" +
            "\t\t\tc.content AS comment_content,\n" +
            "\t\t\tc.publishDate AS comment_publishDate\n" +
            "\tFROM \n" +
            "\t\t\tcampusCirclePosts p\n" +
            "\tLEFT JOIN \n" +
            "\t\t\tpostComments c ON p.postId = c.postId AND c.studentId = #{studentId} -- 这里直接在JOIN条件中过滤用户ID\n" +
            "\tWHERE \n" +
            "\t\t\tc.studentId IS NOT NULL;")
    List<postComments> findCommentsByStudentId(@Param("studentId") Integer studentId);


    /*
    * 用户给帖子点赞
    * */

    @Insert("insert into postlikes(likeId,postId,studentId,status) values(#{likeId},#{postId},#{studentId},#{status}) ")
    int addLikes(@Param("likeId") Integer likeId, @Param("postId") Integer postId, @Param("studentId") Integer studentId, @Param("status") Boolean status);

    /*
    * 用户帖子取消点赞
    * */
    @Insert("delete from postlikes where postId=#{postId} and studentId=#{studentId}")
    int deleteLikes(@Param("postId") Integer postId,@Param("studentId") Integer studentId);

    /*
    * 用户查看自己的点赞信息以及点赞的帖子
    * */
    @Select("\t\t\tSELECT \n" +
            "    p.postId,\n" +
            "    p.studentId AS post_studentId,\n" +
            "    p.content AS post_content,\n" +
            "    p.publishDate AS post_publishDate,\n" +
            "    l.likeId,\n" +
            "    l.studentId AS like_studentId,\n" +
            "    l.status AS like_status \n" +
            "FROM \n" +
            "    campusCirclePosts p \n" +
            "LEFT JOIN \n" +
            "    postLikes l ON p.postId = l.postId AND l.studentId = #{studentId} \n" +
            "WHERE \n" +
            "    l.studentId IS NOT NULL;"
    )
    List<postLikes> findLikesByStudentId(@Param("studentId") Integer studentId);


}
